<?php


class Pages_GeoController extends Zend_Controller_Action {
	
	public function indexAction() {
		$page_id = $this->_request->getParam('page');
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($page_id);
		
		$geo = $page->Page_Coord_Alias;
		
		$this->view->page_id = $page_id;
		$this->view->geo = $geo;
	}
	
	public function createAction() {
		$page_id = $this->_request->getParam('page');
		
		$form = new Pages_Form_Geocoord();
		$form->setAction($this->view->url(array('page' => $page_id),'pages_geo_create'));
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$location = new Pages_Model_Geocoord();
			$location->coord = $values['coord'];
			$location->name = $values['name'];
			$location->page_id = $page_id;
			$location->save();
			
			$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Location created successfully!'));
			return $this->_redirect($this->view->url(array('page' => $page_id), 'pages_geo_index'));
		}
		
		$this->view->form = $form;
	}
	
	public function updateAction() {
		
	}
	
	public function deleteAction() {
		$page_id = $this->_request->getParam('page');
		$id = $this->_request->getParam('id');
		
		$location = Doctrine_Core::getTable('Pages_Model_Geocoord')->find($id);
		$location->delete();
		
		$this->_helper->getHelper('FlashMessenger')
					->addMessage($this->view->translate('Location deleted successfully!'));
		return $this->_redirect($this->view->url(array('page' => $page_id), 'pages_geo_index'));
	}
}